GCM: onMessage() from GCMIntentService is never called [migrated]

Posted by Shrikant on Programmers See other posts from Programmers or by Shrikant
Published on 2012-10-27T11:27:22Z Indexed on 2012/10/27 17:16 UTC
Read the original article Hit count: 196

Filed under:

I am implementing GCM (Google Cloud Messaging- PUSH Notifications) in my application. I have followed all the steps given in GCM tutorial from developer.android.com

My application's build target is pointing to Goolge API 8 (Android 2.2 version).

I am able to get the register ID from GCM successfully, and I am passing this ID to my application server. So the registration step is performed successfully.

Now when my application server sends a PUSH message to my device, the server gets the message as SUCCESS=1 FAILURE=0, etc., i.e. Server is sending message successfully, but my device never receives the message.

After searching alot about this, I came to know that GCM pushes messages on port number 5228, 5229 or 5230.

Initially, my device and laptop was restricted for some websites, but then I was granted all the permissions to access all websites, so I guess these port numbers are open for my device.

So my question is: I never receive any PUSH message from GCM. My onMessage() from GCMIntenService class is never called. What could be the reason?

Please see my following code and guide me accordingly:

I have declared following in my manifest:

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

    <permission
        android:name="package.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <!-- App receives GCM messages. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <!-- GCM connects to Google Services. -->
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />

    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="package.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <receiver
            android:name="com.google.android.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="packageName" />
            </intent-filter>
        </receiver>
        <receiver
            android:name=".ReceiveBroadcast"
            android:exported="false" >
            <intent-filter>
                <action android:name="GCM_RECEIVED_ACTION" />
            </intent-filter>
        </receiver>

        <service
            android:name=".GCMIntentService"
            />


/**
 * @author Shrikant.
 * 
 */
public class GCMIntentService extends GCMBaseIntentService {

    /**
     * The Sender ID used for GCM.
     */
    public static final String SENDER_ID = "myProjectID";

    /**
     * This field is used to call Web-Service for GCM.
     */
    SendUserCredentialsGCM sendUserCredentialsGCM = null;

    public GCMIntentService() {
        super(SENDER_ID);
        sendUserCredentialsGCM = new SendUserCredentialsGCM();
    }

    @Override
    protected void onRegistered(Context arg0, String registrationId) {
        Log.i(TAG, "Device registered: regId = " + registrationId);
        sendUserCredentialsGCM.sendRegistrationID(registrationId);
    }

    @Override
    protected void onUnregistered(Context context, String arg1) {
        Log.i(TAG, "unregistered = " + arg1);
        sendUserCredentialsGCM
                .unregisterFromGCM(LoginActivity.API_OR_BROWSER_KEY);
    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        Log.e("GCM MESSAGE", "Message Recieved!!!");
        String message = intent.getStringExtra("message");
        if (message == null) {
            Log.e("NULL MESSAGE", "Message Not Recieved!!!");
        } else {
            Log.i(TAG, "new message= " + message);
            sendGCMIntent(context, message);
        }
    }

    private void sendGCMIntent(Context context, String message) {
        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("GCM_RECEIVED_ACTION");
        broadcastIntent.putExtra("gcm", message);
        context.sendBroadcast(broadcastIntent);
    }

    @Override
    protected void onError(Context context, String errorId) {
        Log.e(TAG, "Received error: " + errorId);
        Toast.makeText(context, "PUSH Notification failed.", Toast.LENGTH_LONG)
                .show();
    }

    @Override
    protected boolean onRecoverableError(Context context, String errorId) {
        return super.onRecoverableError(context, errorId);
    }
}

© Programmers or respective owner

Related posts about android